package com.knr.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ResponseTextHandler;

public class KRJSUtils {
	
	public static native void enableAccessAcrossDomains() /*-{
		 if (typeof netscape != "undefined") { 
		 
		 	try
		 	{
		 		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		 		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		 		
		 		window.alert("Access enabled");
		 	}
		 	catch( t)
		 	{
		 		window.alert("Permission to access cross domain denied." + t.getMessage());
		 	}
		 		
		 } 
	}-*/;
	
	public static native void replaceString(String origString, String newString) /*-{			
		    // $wnd.alert(origString + newString); 			
		    var elems = $doc.getElementsByTagName("P");
			for (var i=0;i<elems.length;i++) {
				var haystack = elems[i].innerHTML;
				// $wnd.alert(haystack);				
				var repString = haystack.replace(origString, newString);
				elems[i].innerHTML = repString;
			}

	}-*/; 
	
	public static native void findAllParagraphElements(KRParaElems krp) /*-{
			var elems = $doc.getElementsByTagName("P");
			for (var i=0;i<elems.length;i++) {
				var haystack = elems[i].innerHTML;
				krp.@com.knr.client.KRParaElems::addElement(Ljava/lang/String;)(haystack);
			}
	}-*/;
	
	  native static boolean asyncGetImpl(String url, ResponseTextHandler handler) /*-{
	    var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
	    try {
	      alert (url);
	      xmlHttp.open("GET", url, true);
	      	      
	      xmlHttp.onreadystatechange = function() {
//	        if (xmlHttp.readyState == 4) {
	          alert("readystate is 4");
	          delete xmlHttp.onreadystatechange;
	          var localHandler = handler;
	          var responseText = xmlHttp.responseText;
	          handler = null;
	          xmlHttp = null;
	          localHandler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(responseText);
//	        }
	      };
	      
	      return true;
	    }
	    catch (e) {
	      alert(e.message);
	      delete xmlHttp.onreadystatechange;
	      handler = null;
	      xmlHttp = null;
	      return false;
	    }
	  }-*/;
}
